package com.interceptor.interceptordemo.login;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * @author sw
 * @project interceptordemo
 * @description
 * @date 2020/5/8 21:56
 */
@Controller
@RequestMapping("/")
public class LoginController {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @RequestMapping("/toLogin")
    public String toLogin(HttpServletRequest request, Model model){
        model.addAttribute("port",request.getServerPort());
        return "login";
    }
    @RequestMapping("/")
    public String index(HttpServletRequest request, Model model){
        model.addAttribute("port",request.getServerPort());
        return "login";
    }

    /**
     * 登陆
     * @return
     */
    @RequestMapping("/login")
    public String login(HttpServletRequest request, @RequestParam(required = true,name ="userName" ) String userName,
                        @RequestParam(required = true,name = "password") String password,Model model){

        if(!userName.equalsIgnoreCase("admin") ||  !password.equalsIgnoreCase("admin")){
            return "login";
        }
        model.addAttribute("port",request.getServerPort());
        model.addAttribute("userName",userName);
        String sessionValue = redisTemplate.opsForValue().get(userName);
        if(null==sessionValue){
            redisTemplate.opsForValue().set("userName",userName);
        }
        return  "redirect:/resume/list";
    }

}
